//go:build windows

package windows

import (
	"fmt"
	"syscall"
	"unsafe"
)

var (
	shell32                  = syscall.NewLazyDLL("shell32.dll")
	procIsUserAnAdmin        = shell32.NewProc("IsUserAnAdmin")
	kernel32                 = syscall.NewLazyDLL("kernel32.dll")
	procGetCurrentProcessId  = kernel32.NewProc("GetCurrentProcessId")
	procProcessIdToSessionId = kernel32.NewProc("ProcessIdToSessionId")
)

/*
func IsAdminWin() bool {
	// 调用 Windows API IsUserAnAdmin
	ret, _, _ := procIsUserAnAdmin.Call()
	return ret != 0
}
*/

var WindowsHandler = winHandler{}

type winHandler struct{}

func (w *winHandler) IsPrivileged() bool {
	ret, _, _ := procIsUserAnAdmin.Call()
	return ret != 0
}

func (w *winHandler) GetCurrentSessionID() (uint32, error) {
	// 获取当前进程ID
	pid, _, _ := procGetCurrentProcessId.Call()

	// 将进程ID转换为会话ID
	var sessionId uint32
	ret, _, err := procProcessIdToSessionId.Call(
		pid,
		uintptr(unsafe.Pointer(&sessionId)),
	)

	if ret == 0 { // 返回0表示失败
		return 0, fmt.Errorf("ProcessIdToSessionId failed: %v", err)
	}
	return sessionId, nil
}
